package com.ambow.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.event.spi.SaveOrUpdateEventListener;

import com.ambow.Entity.Inventory;
import com.ambow.Entity.Operator;
import com.ambow.Entity.Stock;
import com.ambow.Entity.TextBook;
import com.ambow.service.InventoryService;
import com.ambow.service.TextBookService;
import com.ambow.service.impl.InventoryServiceImpl;
import com.ambow.service.impl.StockMoveServiceImpl;
import com.ambow.service.impl.StockServiceImpl;
import com.ambow.service.impl.TextBookServiceImpl;
import com.ambow.utils.BaseDAO;
import com.ambow.utils.Tools;

/**
 * Servlet implementation class InventoryServlet
 */
@WebServlet("/InventoryServlet")
public class InventoryServlet extends HttpServlet {

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	
	InventoryService inventoryService = new InventoryServiceImpl();
	TextBookService textBookService=new TextBookServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String status = request.getParameter("status");
		System.out.println("status:"+status);
		
		if (status.equals("query")) {//查询教材信息
        queryAll(request,response);
			

		} else if (status.equals("add")) {//添加盘点信息到Inventory
       add(request,response);
			

		} else if(status.equals("detail")) {//查询一本教材的具体信息
			detail(request,response);
			
			
		}else if(status.equals("record")) {//查询inventory中的盘点记录 
			queryRecorad(request,response);
			
		}

	}

	private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Integer id=Integer.valueOf(request.getParameter("id"));
	    System.out.println("11wwwww"+id);
		TextBook textBook=textBookService.queryOne(TextBook.class, id);
		request.setAttribute("textbook", textBook);
		System.out.println("textbook>>>>>"+textBook);
		request.getRequestDispatcher("pages/module/storage/storage004/book_detail.jsp").forward(request,
				response);
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// String cellName=request.getParameter("cellName");
					String amount = request.getParameter("amount");
					System.out.println("amount:::"+amount);
					String action = request.getParameter("action");
					System.out.println("!@#$%%^%&^*()"+action);
					String reason = request.getParameter("reason");
					System.out.println("reason:::"+reason);
				String[] checkbox=request.getParameterValues("checkbox");
				System.out.println("chebox::::"+checkbox.length);
				for (int i = 0; i < checkbox.length; i++) {
					Inventory inventory = new Inventory();
					
					TextBookService textBookService=new TextBookServiceImpl();
					System.out.println("book的id:"+Integer.valueOf(checkbox[i]));
					TextBook textBook=textBookService.queryOne(TextBook.class, Integer.valueOf(checkbox[i]));
					System.out.println("book的name:"+textBook.getName());
						
					inventory.setTextBook(textBook);	
					inventory.setAmount(Integer.valueOf(amount));
					inventory.setAction(action);
					inventory.setDate(new Date());
					inventory.setReason(reason);
					if(request.getSession().getAttribute("op")!=null) {
						inventory.setOperator((Operator)request.getSession().getAttribute("op"));
					}else {
						inventory.setOperator((Operator)BaseDAO.get(Operator.class, 1));
					}
					System.out.println(new Date());
					boolean boo = inventoryService.add(inventory);
					
				}
					request.getRequestDispatcher("pages/module/storage/storage004/pandian_query.jsp").forward(request,
							response);
		
	}

	private void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String cellName = request.getParameter("cellName");
		String amount=request.getParameter("amount");
		String action=request.getParameter("action");
		System.out.println("Action:::::::::queryall"+action);
		String qu[] = new String[] { cellName };
		List<Stock> list = new StockServiceImpl().Query("from Stock as stock where stock.cell.name=?", qu);
		System.out.println("stock:"+list.size());
		
		request.setAttribute("list", list);
		request.setAttribute("cellName", cellName);
		request.setAttribute("amount", amount);
		request.setAttribute("action", action);
		request.getRequestDispatcher("pages/module/storage/storage004/get_pandianbook.jsp").forward(request,
				response);
	}

	private void queryRecorad(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	String operator=request.getParameter("operator");
	String startTime=request.getParameter("startTime");
	String endTime=request.getParameter("endTime");
	
	
	if(operator==""&&startTime==""&&endTime=="") {//全为空
	 List<Inventory> list=inventoryService.query("from Inventory", null); 
	 System.out.println("1全为空dfsgdfgfggfjk"+list.size()); 
	 request.setAttribute("list", list);
	 
	}else if(operator!=""&&startTime==""&&endTime=="") {//操作员不为空
	  String[]	param= {operator};
	  List<Inventory> list=inventoryService.query("from Inventory as inventory where inventory.operator.name=?",param); 
		 System.out.println("2操作员不为空dfsgdfgfggfjk"+list.size()); 
		 request.setAttribute("list", list);	
		 //String sql="from UnitRequirement po where str_to_date(po.publishTime,'%Y-%m-%d')>='"+time+"' and str_to_date(po.publishTime,'%Y-%m-%d')<'"+time1+"'";  
	}else if(operator==""&&startTime!=""&&endTime=="") {//起始时间不为空
		   String[]	param= {startTime};
		  List<Inventory> list=inventoryService.query("from Inventory where date>=?",param); 
			 System.out.println("3起始时间不为空dfsgdfgfggfjk"+list.size()); 
			 request.setAttribute("list", list);	
		
	}else if(operator==""&&startTime==""&&endTime!="") {//终止时间不为空
		 String[]	param= {endTime};
		  List<Inventory> list=inventoryService.query("from Inventory where date<=?",param); 
			 System.out.println("4终止时间不为空dfsgdfgfggfjk"+list.size()); 
			 request.setAttribute("list", list);
	}else if(operator!=""&&startTime!=""&&endTime=="") {//操作员姓名和起始时间不为空
		
		String[]	param= {operator,startTime};
		  List<Inventory> list=inventoryService.query("from Inventory as inventory where inventory.operator.name=?and inventory.date>=?",param); 
			 System.out.println("5操作员姓名和起始时间不为空dfsgdfgfggfjk"+list.size()); 
			 request.setAttribute("list", list);
	}else if(operator!=""&&startTime==""&&endTime!="") {//操作员姓名和终止时间不为空
		
		String[]	param= {operator,endTime};
		  List<Inventory> list=inventoryService.query("from Inventory as inventory where inventory.operator.name=?and inventory.date<=?",param); 
			 System.out.println("6操作员姓名和终止时间不为空dfsgdfgfggfjk"+list.size()); 
			 request.setAttribute("list", list);
	}else if(operator==""&&startTime!=""&&endTime!="") {//起始时间和终止时间不为空
		
		String[]	param= {startTime,endTime};
		  List<Inventory> list=inventoryService.query("from Inventory as inventory where inventory.date between ? and ?",param); 
			 System.out.println("7起始时间和终止时间不为空dfsgdfgfggfjk"+list.size()); 
			 request.setAttribute("list", list);
	}else if(operator!=""&&startTime!=""&&endTime!="") {//全部不为空
		
		String[]	param= {operator,startTime,endTime};
		  List<Inventory> list=inventoryService.query("from Inventory as inventory where inventory.operator.name=? and inventory.date between ? and ?",param); 
			 System.out.println("8全部不为空dfsgdfgfggfjk"+list.size()); 
			 request.setAttribute("list", list);
	}
	
		request.getRequestDispatcher("pages/module/storage/storage004/get_query_pandian.jsp").forward(request, response);
	}
	
}
